﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Question.Entiry;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;

public partial class sysadmin_QuestionManager_AddQuestionResourceMini : PowerCreator.LMS.Web.Base.BasePage
{
    //ManagerQuestionResourceInfo objManagerQuestionResourceInfo = new ManagerQuestionResourceInfo();
    ManagerQuestion objQuestion = new ManagerQuestion();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Pageload();
            ViewState["UserID"] = 10000;
            InitPage();
        }
        
    }
    protected override void InitPage()
    {
        lbl_InfoTitle.Text = GetLanageValueByKey("lab_AddQuestionResourceMini_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_Name.Text = GetLanageValueByKey("lab_AddQuestionResourceMini_Name", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_Type.Text = GetLanageValueByKey("lab_AddQuestionResourceMini_Type", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_SelectFile.Text = GetLanageValueByKey("lab_AddQuestionResourceMini_SelectFile", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_txt.Text = GetLanageValueByKey("lab_AddQuestionResourceMini_MsgText", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);


        base.InitPage();
    }
    private void Pageload()
    {
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("msg_AddQuestionResourceMini_Video", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "1";
        ddlSystemType.Items.Insert(0, li);
        ListItem li1 = new ListItem();
        li1.Text = GetLanageValueByKey("msg_AddQuestionResourceMini_download", PowerCreator.LMS.Language.LanagetItemType.Page);
        li1.Value = "2";
        ddlSystemType.Items.Add(li1);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {


        try
        {
            HttpPostedFile objPostFile = File1.PostedFile;
            if (objPostFile.FileName == "")
            {
                Script.Alert(this, "msg", GetLanageValueByKey("Msg_QuestionToIn_PleaseSelectFile", PowerCreator.LMS.Language.LanagetItemType.Page));
                return;
            }


            FileInfo objFileInfo = new FileInfo(objPostFile.FileName);
            string guid = Guid.NewGuid().ToString();
            string newFileName = guid + objFileInfo.Extension;

            string path = Server.MapPath(WebUtils.WebRoot + "/QeustionResource/" + newFileName);
            if (!Directory.Exists(Server.MapPath(WebUtils.WebRoot + "/QeustionResource")))
            {
                Directory.CreateDirectory(Server.MapPath(WebUtils.WebRoot + "/QeustionResource"));
            }
            objPostFile.SaveAs(path);
            VOQuestionResource objQuestionResourceInfo = new VOQuestionResource();
            if (txtQuestionTypeName.Text == "")
            {
                objQuestionResourceInfo.QuestionResFileName = objFileInfo.Name;
            }
            else
            {
                objQuestionResourceInfo.QuestionResFileName = txtQuestionTypeName.Text;
            }

            objQuestionResourceInfo.QuestionResGuID = guid;
            objQuestionResourceInfo.QuestionResFileType = int.Parse(ddlSystemType.SelectedValue);
            objQuestionResourceInfo.QuestionResUrl = WebUtils.WebRoot + "/QeustionResource/" + newFileName;
            objQuestionResourceInfo.QuestionResUploadFileName = newFileName;
            objQuestionResourceInfo.QuestionResCreateID = int.Parse(ViewState["UserID"].ToString());
            objQuestionResourceInfo.QuestionResCreateTime = DateTime.Now;
            objQuestionResourceInfo.QuestionResUpdateTime= DateTime.Now;
            if (objQuestion.CreateQuestionResource(objQuestionResourceInfo))
            {
                lab_Msg.Visible = true;
                lab_txt.Visible = true;
                lab_ButtonCopy.Visible = true;
                txt_Url.Visible = true;
                txt_Url.Value = WebUtils.WebRoot + "/sysadmin/QuestionManager/OpenQuestionResource.aspx?ID=" + objQuestionResourceInfo.QuestionResGuID;
                lab_ButtonCopy.Text = "<input id=\"btn_Copy\" type=\"button\" onclick=\"oCopy(txt_Url);\" Class=\"mybuttons\" value=\"复制地址\" />";
            }
            else
            {
                Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
            }
            

        }
        catch (Exception ex)
        {
            Script.Alert(ex.Message);
        }

    }

}
